package com.xxl.dao.impl;

import com.xxl.dao.VerifyUserDao;
import com.xxl.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class VerifyUserDaoImpl implements VerifyUserDao {
    Connection conn;
    PreparedStatement preparedStatement;
    ResultSet resultSet;
    @Override
    public boolean verifyUserNameAndPassword(String userName, String password){
        try {
            try {
                conn = new JDBCUtil().init();
            } catch (SQLException throwables) {
                System.out.println(throwables);
            }
            String sql = "SELECT id FROM user WHERE username=? AND password=?";
            preparedStatement = conn.prepareStatement(sql);
            int index = 1;
            preparedStatement.setString(index++,userName);
            preparedStatement.setString(index,password);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                return true;
            }
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        } catch (SQLException throwables) {
            System.out.println(throwables);
        }finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                System.out.println(throwables);
            }
        }
        return false;
    }
}
